package jdir.dialog;

import static jdir.util.LogUtil.begin;
import static jdir.util.LogUtil.end;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import jdir.Control;
import jdir.dialog.exceptions.OutOfRowCountException;
import jdir.exceptions.NoDirectoryException;
import jdir.exceptions.NotSufficientRightsException;
import jdir.util.PathGen;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class will build the FileList out of the list itself, the {@link ToolBarPanel} and
 * a combo box for the path.
 * @author Marco Peroverde
 */
@SuppressWarnings("unused")
public class FileListPanel extends JPanel implements KeyListener
{
	private static Log				LOG			= LogFactory.getLog(FileListPanel.class);
	private static Boolean			DEBUG		= LOG.isDebugEnabled();	
	private static Boolean			TRACE		= LOG.isTraceEnabled();
	
	private static final long	serialVersionUID	= 4074613578608672799L;
	
	boolean isLeftSide;
	JFrame parent;
	JTable 	fileList;
	JComboBox pathList;
	JButton	sideButton;
	ToolBarPanel toolBar;
	JSplitPane fileListAndPathSplitter;
	JSplitPane buttonAndFileLiSplitter;
	JScrollPane scrollPane;
	JPanel listAndPathPane;
	
	ArrayList<Integer> keyCodeList = new ArrayList<Integer>();
	
	/**
	 * Generates the filelist with toolbar and combobox. If KeyTyped Events are fired the most of
	 * them will be forwarded to control. Except the few which are used to interact with the
	 * list. This are: <br>
	 * <ul> <li>KeyEvent.VK_LEFT</li> 
	 * <li>KeyEvent.VK_UP</li>
	 * <li>KeyEvent.VK_RIGHT</li>
	 * <li>KeyEvent.VK_DOWN</li>
	 * <li>KeyEvent.VK_KP_LEFT</li>
	 * <li>KeyEvent.VK_KP_UP</li>
	 * <li>KeyEvent.VK_KP_RIGHT</li>
	 * <li>KeyEvent.VK_KP_DOWN</li>
	 * <li>KeyEvent.VK_SHIFT</li></ul>
	 * @param isLeftSide Needs to be true if the list will be shown on the left 
	 * side of the screen. Otherwise orientation will be wrong inside of the list.
	 * @param parent Handle to the parent Frame
	 */
	public FileListPanel(boolean isLeftSide, int height, int width, JFrame parent)
	{
		if(DEBUG)
			begin(LOG, "");
		this.isLeftSide = isLeftSide;
		this.parent = parent;
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		height = (int)Math.round(((double)2/3)*700);
		File file[] = null;
		try
		{
			file = Control.getInstance().generateFileListFor(new PathGen(PathGen.DEFAULT, "/"));
		} catch (NotSufficientRightsException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoDirectoryException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		fileList = new JTable(new FileDataModel(file));
		fileList.setBackground(new Color(110,0,0));
		scrollPane = new JScrollPane(fileList);
		//Set the filelist auf minimum of 90% of panel height
		scrollPane.setPreferredSize(new Dimension(width, (int)(height * 0.9)));
		
		//TODO Needs an actionlistener...
		pathList = new JComboBox();
		pathList.setEditable(true);
		pathList.setPreferredSize(new Dimension(width, (int)(height * 0.05)));
		
		toolBar = new ToolBarPanel(!isLeftSide, width, (int)(height * 0.05));
		//toolBar.setPreferredSize(new Dimension(this.getWidth(), (int)(height * 0.05) ));
		
		this.add(scrollPane);
		this.add(pathList);
		this.add(toolBar);
		fileList.addKeyListener(this);
		
		//Mask of Keycodes which will not be forwarded to Control
		keyCodeList.add(KeyEvent.VK_LEFT);
		keyCodeList.add(KeyEvent.VK_UP);
		keyCodeList.add(KeyEvent.VK_RIGHT);
		keyCodeList.add(KeyEvent.VK_DOWN);
		keyCodeList.add(KeyEvent.VK_KP_LEFT);
		keyCodeList.add(KeyEvent.VK_KP_UP);
		keyCodeList.add(KeyEvent.VK_KP_RIGHT);
		keyCodeList.add(KeyEvent.VK_KP_DOWN);
		keyCodeList.add(KeyEvent.VK_SHIFT);
		
		if(DEBUG)
			end(LOG, "");
	}

	/**
	 * Generates a list of all selected Files. This includes directories.
	 * @return the generatet list as ArrayList
	 */
	public List<File> getSelectedFiles() {
		int rows[] = fileList.getSelectedRows();
		int selectedRowCount = rows.length;
		ArrayList<File> selectedFiles = new ArrayList<File>();
		FileDataModel dataModel = (FileDataModel)fileList.getModel();
		for(int i = 0; i < selectedRowCount; i++) {
			try {
				selectedFiles.add(dataModel.getFileAt(rows[i]));
			} catch (OutOfRowCountException e) {
				LOG.error(e.getMessage());
			}
		}
			
		return selectedFiles;		
	}
	
	@Override
	public boolean hasFocus()
	{
		return fileList.hasFocus();
		
	}
	
	@Override
	public void keyPressed(KeyEvent e)
	{
		//Control.getInstance().performCommand("", e.getKeyCode());
		
	}


	@Override
	public void keyReleased(KeyEvent e)
	{
	
		if(keyCodeList.contains(e.getKeyCode()))
			return;
		Control.getInstance().performCommand("", e.getKeyCode(), (JDirMainDlg) parent);
		
	}


	@Override
	public void keyTyped(KeyEvent e)
	{
		//Control.getInstance().performCommand("", e.getKeyCode());
		
	}
}
